﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Tour_ctrTourCategory : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.setTitle();
            this.BindData(1);
        }
    }
    protected void setTitle()
    {
        int nTourCat = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_CAT]);
        int nSalesOff= ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_SALEOFF]);
        int nPopular = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_POPULAR]);
       

        if (nTourCat > 0 && nSalesOff == 0 && nPopular == 0)
        {
            TourCategory objTourCat = new TourCategory();
            objTourCat.LoadById(nTourCat);
            switch (nCurrentLang)
            {
                case 1:
                    lblCategoryName.Text = objTourCat.Data.TourCategoryName1;
                    break;
                case 2:
                    lblCategoryName.Text = objTourCat.Data.TourCategoryName2;
                    break;
                case 3:
                    lblCategoryName.Text = objTourCat.Data.TourCategoryName3;
                    break;
            }
        }
        if (nTourCat == 0 && nSalesOff > 0 && nPopular == 0)
        {
            SaleOff objSaleOff = new SaleOff();
            objSaleOff.LoadById(nSalesOff);
            switch (nCurrentLang)
            {
                case 1:
                    lblCategoryName.Text = objSaleOff.Data.Title1;
                    break;
                case 2:
                    lblCategoryName.Text = objSaleOff.Data.Title2;
                    break;
                case 3:
                    lblCategoryName.Text = objSaleOff.Data.Title3;
                    break;
            }
        }

        if (nTourCat == 0 && nSalesOff == 0 && nPopular > 0)
        {
            TourPopular objTourPopular = new TourPopular();
            objTourPopular.LoadById(nPopular);
            switch (nCurrentLang)
            {
                case 1:
                    lblCategoryName.Text = objTourPopular.Data.Description1;
                    break;
                case 2:
                    lblCategoryName.Text = objTourPopular.Data.Description2;
                    break;
                case 3:
                    lblCategoryName.Text = objTourPopular.Data.Description3;
                    break;
            }
        }

        
    }

    private int GetCountry()
    {
        int nCountry = 0;
        nCountry = ConvertData.ConvertToInt(Request.QueryString[Config.COUNTRY]);
        if (nCountry == 0)
            nCountry = Config.COUNTRY_DEFAULT;
        return nCountry;
    }

    private void BindData(int CurrentPageIndex)
    {
        int nStartDestinationID = 0;
        int nEndDestionaion = 0;
        int nTourDes = 0;//ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_DES]);

        string sSearch = ConvertData.ConvertToString(Request.QueryString[Config.KEYWORD]);//"";
        int nSaleOff = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_SALEOFF]);//0;
        int nDuration = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_DURATION]);//0;

        string sTourCat = ConvertData.ConvertToString(Request.QueryString[Config.TOUR_CAT]);
        if (ConvertData.ConvertToInt(sTourCat) < 1)
            sTourCat = "";

        int nStatus = 1;
        int nPageCount = 0;
       
        int nisCustomer = -1;

        int nCountry = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_COUNTRY]);//0;
        int nRegion = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_REGION]);//0;
        int nTourPopular = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_POPULAR]);//0;
        if (ConvertData.ConvertToString(Request.QueryString[Config.TOUR_ISCUSTOMERTOUR]) != "")
        {
            nisCustomer = ConvertData.ConvertToInt(Request.QueryString[Config.TOUR_ISCUSTOMERTOUR]);//0;
        }
      
        try
        {
            DataTable dtb = new DataTable();
            if (ConvertData.ConvertToInt(Request.QueryString[Config.SEARCH_TYPE]) == 3)
            {
                TourPopularDescription objTourPopularDesc = new TourPopularDescription();
                dtb = objTourPopularDesc.Search(nTourPopular, 0, nDefaultLanguage, nCurrentLang, CurrentPageIndex, nPageSize, ref nPageCount);
            }
            else
            {
                Tours objTour = new Tours();
                dtb = objTour.Search(0, 0, sSearch, nStartDestinationID, nEndDestionaion, nTourPopular, nSaleOff, nTourDes, nDuration, sTourCat, nDefaultLanguage, nCurrentLang, nisCustomer, nStatus, CurrentPageIndex, 16, ref nPageCount);
            }
            rptTour.DataSource = dtb;
            rptTour.DataBind();

            Pager.ItemCount = nPageCount;
            VisiblePaging(nPageCount);
            if (dtb.Rows.Count > 0)
                tblWarning.Visible = false;
            else
                tblWarning.Visible = true;
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    protected void rptTour_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnTourImage = (ImageButton)e.Item.FindControl("btnTourImage");
            HyperLink lnkTourTitle = (HyperLink)e.Item.FindControl("lnkTourTitle");
            HyperLink lnkReadmore = (HyperLink)e.Item.FindControl("lnkReadmore");
            Label lblDuration = (Label)e.Item.FindControl("lblDuration");
            Literal ltrTour = (Literal)e.Item.FindControl("ltrTour");

           string sInitContent = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
            if (sInitContent.Length >170)
                sInitContent = ConvertData.TruncateString(sInitContent, 170) + "...";

            ltrTour.Text = sInitContent;

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TourID"));
            lnkTourTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.TOUR_DETAIL + "?" + Config.TOUR_ID + "=" + nID + "&cid=" + this.GetCountry();
            lnkTourTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title"));
            lnkReadmore.NavigateUrl = Config.ROOT + Pages.FrontEnds.TOUR_DETAIL + "?" + Config.TOUR_ID + "=" + nID + "&cid=" + this.GetCountry();
            lnkReadmore.Text = MultipleLanguage.LNK_READMORE[nCurrentLang - 1];

            btnTourImage.ImageUrl = Config.ROOT + Config.IMAGE_TOUR_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            btnTourImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourID"));
            lblDuration.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "DurationDescription2"));
        }
    }
    protected void rptTour_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.TOUR_DETAIL + "?" + Config.TOUR_ID + "=" + nID + "&cid=" + this.GetCountry();
                Response.Redirect(sDetail);
                break;
        }
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }


    private int nPageSize = 12;
    private void initDataPaging()
    {
        Pager.PageSize = nPageSize;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        if (intPagecount > Pager.PageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
}
